package com.weedsdream.compose

import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.weedsdream.base.R

/**包含文字的图标按钮，默认大小43.dp
 * @param iconId 图标编号
 * @param iconTint 图标过滤色
 * @param tag 文字标签
 * @param tagColor 文字标签颜色
 *
 * @param iconSize 图标大小
 * @param tagSize 标签文字大小
 *
 * @param chose 当前图标是否被选中？
 * @param choseIconId 选中时变化的图标
 * @param choseIconTint 选中时变化的图标颜色
 * @param choseTag 选中时变化的标签
 * @param choseTagColor 选中时变化的标签颜色
 *
 * @param modifier 补充修饰符，作用于IconButton整体
 * @param enable 启用状态，不启用时图标为灰色
 * @param onclick 点击事件*/
@Preview
@Composable
fun IconButton(
    iconId: Int = R.drawable.tp_logo,
    iconTint: Color = Color.Unspecified,
    tag: String? = "按钮",
    tagColor: Color = Color.Unspecified,
    iconSize: Dp = 25.dp,
    tagSize: TextUnit = 15.sp,
    chose: Boolean = false,
    choseIconId: Int = iconId,
    choseIconTint: Color = iconTint,
    choseTag: String = tag ?: "",
    choseTagColor: Color = tagColor,
    modifier: Modifier = Modifier,
    enable: Boolean = true,
    onclick: () -> Unit = {}
) {
    Box(modifier) {
        if (enable) Spacer(
            Modifier
                .size(43.dp)
                .align(Alignment.Center)
                .clip(CircleShape)
                .pointerInput(Unit) {
                    detectTapGestures {
                        onclick()
                    }
                })
        Column(Modifier.align(Alignment.Center)) {
            Icon(
                painter = painterResource(id = if (!chose) iconId else choseIconId),
                contentDescription = tag, tint = if (enable) {
                    if (!chose) iconTint else choseIconTint
                } else Color.Gray,
                modifier = Modifier
                    .size(iconSize)
                    .align(Alignment.CenterHorizontally)
            )
            if (tag != null) Text(
                text = if (!chose) tag else choseTag, fontSize = tagSize, color = if (enable) {
                    if (!chose) tagColor else choseTagColor
                } else Color.Gray, modifier = Modifier.align(Alignment.CenterHorizontally)
            )
        }
    }
}